import { _decorator, Component, Label, macro, Node } from 'cc';
import { gameMgr } from './mainScene/gameMgr';
const { ccclass, property } = _decorator;

@ccclass('TimeCountTable')
export class TimeCountTable extends Component {
    @property(Label)
    label: Label = null;

    static ins:TimeCountTable;
    public elapsedTime: number = 0;
    private counting: boolean = false;

    protected onLoad(): void {
        TimeCountTable.ins=this;
    }

    start() {
        // 在开始时启动计时
        this.startTimer();
    }

    startTimer() {
        // 设置计时器为启动状态
        this.counting = true;

        // 使用schedule函数每秒执行一次updateTimer方法
        this.schedule(this.updateTimer, 1, macro.REPEAT_FOREVER, 0);
    }

    updateTimer(dt: number) {
        // 如果计时器处于启动状态，更新已经流逝的时间
        if (this.counting&&gameMgr.ins.isGamePause==false) {
            this.elapsedTime += dt;
            // 在这里可以更新显示计时的文本
            this.label.string = this.formatTime(this.elapsedTime);
        }
    }

    stopTimer() {
        // 设置计时器为停止状态
        this.counting = false;
        // 取消schedule
        this.unschedule(this.updateTimer);
    }

    formatTime(seconds: number): string {
        // 将秒数格式化为分:秒的形式
        const minutes = Math.floor(seconds / 60);
        const remainingSeconds = Math.floor(seconds % 60);
        return `${minutes}分:${remainingSeconds < 10 ? '0' : ''}${remainingSeconds}秒`;
    }

}


